package tmproject.hlhj.fhp.tmvote.customView

import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.widget.PopupWindow
import android.widget.RelativeLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import kotlinx.android.synthetic.main.search_pop.view.*
import tmproject.hlhj.fhp.tmvote.R
import tmproject.hlhj.fhp.tmvote.beans.SearchBean
import tmproject.hlhj.fhp.tmvote.log
import tmproject.hlhj.fhp.tmvote.network.Urls

/**
 * Created by Never Fear   on 2018\8\7 0007.
Never More....
 */
class SearchPop(private val context:Context,private val mWidth:Int) :PopupWindow(context) {
    private lateinit var adp:BaseQuickAdapter<SearchBean.DataBean.ListBean,BaseViewHolder>
    private val datas=ArrayList<SearchBean.DataBean.ListBean>()
    init {
        this.width=mWidth+2
        this.height=RelativeLayout.LayoutParams.WRAP_CONTENT
        this.isOutsideTouchable=true
        this.isFocusable=true
        initPop()
    }

    private fun initPop() {
        contentView=LayoutInflater.from(context).inflate( R.layout.search_pop,null)

        adp=object :BaseQuickAdapter<SearchBean.DataBean.ListBean,BaseViewHolder>(R.layout.search_item,datas){
            override fun convert(helper: BaseViewHolder?, item: SearchBean.DataBean.ListBean?) {
                helper?.let {
                    it.setText(R.id.itemName,item?.competitor_name)
                    it.setText(R.id.itemRank,"NO.${item?.competitor_id.toString()}")
                }
            }
        }
        contentView.recyclerview.layoutManager=LinearLayoutManager(context)
        contentView.recyclerview.adapter=adp
        adp.setOnItemClickListener { adapter, view, position ->
            val bundle = Bundle()
            bundle.putString("name",datas[position].competitor_name)
            bundle.putString("num",datas[position].competitor_id.toString())
            bundle.putString("img", Urls.Base_Url+datas[position].avatar)
            bundle.putString("des",datas[position].profile)
            bundle.putString("id",datas[position].competitor_id.toString())
            log("fhp","一次")
            InfoDialog(context,bundle,0).show()
            dismiss()
        }
    }

    public fun setdata(searchBean: SearchBean?){
        searchBean?.let {
            datas.clear()
            adp.addData(it.data.list)
        }
    }
}